package com.ist.viewpagerhelper.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ist.viewpagerhelper.ContractUtils;
import com.ist.viewpagerhelper.bean.ViewPagerBean;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * Created by zhengshaorui on 2017/11/5.
 * csdn: http://blog.csdn.net/u011418943
 */

public class BannerStyle {


    private int mCount;
    private int mLeftMargin;
    private Context mContext;
    private int mSelector;
    private float mAlpha_min;
    private View mOpenView;
    private int mMode;
    private static final String TAG = "zsr";
    public BannerStyle(Context mContext,int mSelector, boolean isScale,
                       int mLeftMargin, float mAlpha_min, float mScale_max) {
        this.mContext = mContext;
        this.mLeftMargin = mLeftMargin;
        this.mSelector = mSelector;
        this.mAlpha_min = mAlpha_min;


    }

    public void setLoopData(ViewGroup viewGroup,ViewPagerBean bean) {

        if (bean.viewPager != null){
            mCount = bean.count;
            //这里加小圆点
            for (int i = 0; i < mCount; i++) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                if (i == mCount - 1){ //防止indicator的宽度为wrap_content，放大后被遮盖住的问题
                    params.setMargins(mLeftMargin,0,mLeftMargin,0);
                }else{
                    params.setMargins(mLeftMargin,0,0,0);
                }

                ImageView imageView = new ImageView(mContext);
                imageView.setBackgroundResource(mSelector);
                imageView.setLayoutParams(params);
                imageView.setAlpha(mAlpha_min);

                viewGroup.addView(imageView);
            }
            mMode = bean.mode;
        }
        if (bean.openview != null){
            mOpenView = bean.openview;
        }

    }

    public void getPageSeleted(int position){
        if (mMode != 0 && mMode == ContractUtils.GLIDE_MODE) {
            showStartView(position);
        }
    }


    /**
     * 显示最后一页的状态
     * @param position
     */
    private void showStartView(int position) {
        // 最后一页
        if (position == mCount - 1) {
            if (mOpenView != null) {
                mOpenView.setVisibility(VISIBLE);
                ObjectAnimator animator = ObjectAnimator.ofFloat(mOpenView,
                        "alpha", 0, 1);
                animator.setDuration(500);
                animator.setInterpolator(new LinearInterpolator());
                animator.start();
            }
        } else {
            if (mOpenView != null) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(mOpenView,
                        "alpha", 1, 0);
                animator.setDuration(300);
                animator.setInterpolator(new LinearInterpolator());
                animator.start();
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        mOpenView.setVisibility(GONE);
                    }
                });
            }
        }
    }

    public int getRealPosition(int mode,int postition){
        if (mode == ContractUtils.LOOP_TAIL_MODE){
            return postition - 1;
        }
        if (mode == ContractUtils.LOOP_MAX_MODE){
            return postition % mCount;
        }
        return postition;
    }

}
